package com.google.code.jcooky.rpc.server;

import java.util.HashMap;
import java.util.Map;

public class ServiceRegistry {
	private final static String INTERFACE_NAME = "Iface"; // Service가 상속받는 인터페이스의 Keyword
	
	private Map<String, Object> services = new HashMap<String, Object>();
	
	public void add(Object instance) {
		Class<?> ifaceClass = null;
		for(Class<?> eachIfaceClass : instance.getClass().getInterfaces()) {
			if(eachIfaceClass.getName().endsWith("$"+INTERFACE_NAME)) {
				ifaceClass = eachIfaceClass;
				break;
			}
		}
		
		if(ifaceClass == null) {
			throw new RuntimeException("Service클래스는 "+INTERFACE_NAME+" 을 상속받아야 합니다.");
		}
		else {
			services.put(ifaceClass.getName(), instance);
		}
	}
	
	public Object get(String ifaceName) {
		return services.get(ifaceName);
	}
}
